/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package utils.parser;

import model.Node;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import repco.RepCo;
import utils.stemmer.SnowballProgram;

/**
*
* @author falxa
*/
public class DocumentHandler extends DefaultHandler
{
    
    private class XpathChild
    {
        String name;
        int children;

        public XpathChild(String name, int children) 
        {
            this.name = name;
            this.children = children;
        }
    }
    
    //résultats de notre parsing
    private StringBuffer buffer;
    private RepCo rc;

    private int currentNode = 0;
    private ArrayList<String> currentXpath  = new ArrayList<>();
    private ArrayList<HashMap<String,Integer>> childXpath = new ArrayList<>();
    private int currentDocument;
    
    
    
    public DocumentHandler(RepCo rc) 
    {
        super();
        this.rc = rc;
    }
    
    private void addXpathNode(String qName)
    {
        currentXpath.add(qName);
        childXpath.add(new HashMap<String,Integer>());
        
        int pathsize = currentXpath.size();
        if (pathsize > 1)
        {
            if (childXpath.get(pathsize-2).containsKey(qName))
                childXpath.get(pathsize-2).put(qName, childXpath.get(pathsize-2).get(qName)+1);
            else
                childXpath.get(pathsize-2).put(qName, 1);
        }
    }
    
    private void delXpathNode(String qName)
    {
        int pathsize = currentXpath.size();
        if (currentXpath.get(pathsize-1).equals(qName))
        {
            currentXpath.remove(pathsize-1);
            childXpath.remove(pathsize-1);
        }
    }
    
    private String getXpath()
    {
        String xpath = "/";
        
        for (int i=0; i<currentXpath.size(); i++)
        {
            xpath += currentXpath.get(i);
            if (i==0)
                xpath += "[1]/";
            else
                xpath += "["+childXpath.get(i-1).get(currentXpath.get(i))+"]/";
        }
        
        xpath = xpath.substring(0, xpath.length()-1);
        
        return xpath;
    }

    //détection d'ouverture de balise
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
        currentNode = rc.getNodes().size();
        addXpathNode(qName);
        Node n = new Node(currentNode, currentDocument, qName, getXpath());
        rc.addNode(n);
    }

    //détection fin de balise
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException
    {
        delXpathNode(qName);
    }

    //détection de caractères
    @Override
    public void characters(char[] ch,int start, int length) throws SAXException
    {
        String lecture = new String(ch,start,length);
        String terms[];
        
        //lecture = lecture.replaceAll("(\\p{Punct}|\\p{Space}|«|»|’|…)+", " ");
        lecture = lecture.replaceAll("(\\p{Punct}|[^(\\p{L}|\\p{N})|œ|Œ])+", " ");
        
        terms = lecture.split(" +");
        
        for (int i=0; i<terms.length; i++)
        {
        	rc.addTerm(terms[i], currentNode, i);
        }
        
        if(buffer != null) buffer.append(lecture);       
    }
    
    //début du parsing
    @Override
    public void startDocument() throws SAXException 
    {
        currentDocument = rc.getDocuments().size()-1;
    }
    
    //fin du parsing
    @Override
    public void endDocument() throws SAXException 
    {
    }
}
